package com.point.moments.oss.biz.service.impl;

import com.point.framework.common.response.Response;
import com.point.moments.oss.biz.service.FileService;
import com.point.moments.oss.biz.strategy.FileStrategy;
import io.minio.errors.*;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Service
@Slf4j
@RefreshScope
public class FileServiceimpl implements FileService  {

    @Resource
    private FileStrategy fileStrategy;
    @Value("${storage.type}")
    private String storagyType;
    private static String BUCKET_NAME = "moments";
    @Override
    @RefreshScope
    public Response<?> uploadFile(MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        // 上传文件
        if(storagyType.equals("aliyun")){
            BUCKET_NAME = "moments-xiaohashu";
        }
        log.info("-----------BUCKET_NAME:{}",BUCKET_NAME);
        String url = fileStrategy.uploadFile(file, BUCKET_NAME);

        return Response.success(url);
    }
}
